<template>
  <el-dialog
    :title="!dataForm.orcId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单号" prop="orcOrderSn">
      <el-input v-model="dataForm.orcOrderSn" placeholder="订单号"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="orcCoId">
      <el-input v-model="dataForm.orcCoId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="商品数量" prop="orcCommodityNum">
      <el-input v-model="dataForm.orcCommodityNum" placeholder="商品数量"></el-input>
    </el-form-item>
    <el-form-item label="商品单个价格" prop="orcCommodityPrice">
      <el-input v-model="dataForm.orcCommodityPrice" placeholder="商品单个价格"></el-input>
    </el-form-item>
    <el-form-item label="商品购买的状态" prop="orcState">
      <el-input v-model="dataForm.orcState" placeholder="商品购买的状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orcId: 0,
          orcOrderSn: '',
          orcCoId: '',
          orcCommodityNum: '',
          orcCommodityPrice: '',
          orcState: ''
        },
        dataRule: {
          orcOrderSn: [
            { required: true, message: '订单号不能为空', trigger: 'blur' }
          ],
          orcCoId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          orcCommodityNum: [
            { required: true, message: '商品数量不能为空', trigger: 'blur' }
          ],
          orcCommodityPrice: [
            { required: true, message: '商品单个价格不能为空', trigger: 'blur' }
          ],
          orcState: [
            { required: true, message: '商品购买的状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orcId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orcId) {
            this.$http({
              url: this.$http.adornUrl(`/sulingoxqg/ordercommodity/info/${this.dataForm.orcId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orcOrderSn = data.orderCommodity.orcOrderSn
                this.dataForm.orcCoId = data.orderCommodity.orcCoId
                this.dataForm.orcCommodityNum = data.orderCommodity.orcCommodityNum
                this.dataForm.orcCommodityPrice = data.orderCommodity.orcCommodityPrice
                this.dataForm.orcState = data.orderCommodity.orcState
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sulingoxqg/ordercommodity/${!this.dataForm.orcId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orcId': this.dataForm.orcId || undefined,
                'orcOrderSn': this.dataForm.orcOrderSn,
                'orcCoId': this.dataForm.orcCoId,
                'orcCommodityNum': this.dataForm.orcCommodityNum,
                'orcCommodityPrice': this.dataForm.orcCommodityPrice,
                'orcState': this.dataForm.orcState
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
